package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.PageParams;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session) {
        // System.out.println("employee   " + employee);
        Employee loginEmployee = employeeService.login(employee);
        if (loginEmployee == null) {
            return R.error("账号或者密码错误,或者账号停用了");
        } else {
            session.setAttribute("employee", loginEmployee);
            return R.success(loginEmployee);
        }

    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */

    @PostMapping("/logout")
    public R logout(HttpSession session) {
        //让session失效，，同时释放空间
        session.invalidate();
        return R.success("退出成功");
    }

    /**
     * 添加用户
     *
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R save(@RequestBody Employee employee, HttpServletRequest request) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
     /*   employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/
        //获取当前登录用户的id
     /*   Employee employee1 = (Employee) request.getSession().getAttribute("employee");
        Long eid = employee1.getId();
        employee.setCreateUser(eid);
        employee.setUpdateUser(eid);*/
        employeeService.save(employee);
        return R.success("新增员工成功");

    }


    /**
     * 分页查询
     */


    @GetMapping("/page")
    public R findPage(PageParams pageParam) {
       /* System.out.println("传过来的参数");
        System.out.println(pageParam);*/
        Page<Employee> pageE = employeeService.findPage(pageParam);
        return R.success(pageE);
    }

    /**
     * 更新状态
     *
     * @param employee
     */
    @PutMapping
    public R updateStatus(@RequestBody Employee employee, HttpSession session) {
        System.out.println("employee");
        System.out.println(employee);
        //取出当前用户的id
        /*Employee loginE = (Employee) session.getAttribute("employee");
        Long id = loginE.getId();
        int rows = employeeService.updateStatus(employee, id);*/
        int rows=employeeService.update(employee);
        if(rows>0){
            return R.success("更新成功");
        }else {
            return R.error("更新失败");
        }
    }

}
